package com.sqx.modules.postPush.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.postPush.dao.PostPushDao;
import com.sqx.modules.postPush.entity.PostPush;
import com.sqx.modules.postPush.service.PostPushService;
import com.sqx.modules.resumes.vo.CityVo;
import com.sqx.modules.resumes.vo.LetterCity;
import com.sqx.modules.postPush.vo.PostPushVo;
import com.sqx.modules.utils.SenInfoCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 岗位推送
 */
@Slf4j
@Service
public class PostPushServiceImpl extends ServiceImpl<PostPushDao, PostPush> implements PostPushService {
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;
    @Override
    public Result savePostPush(PostPush postPush) {
        postPush.setStatus(0);
        CommonInfo isAuth = commonInfoService.findOne(344);
        UserEntity userEntity = userService.getById(postPush.getUserId());
        if ("是".equals(isAuth.getValue())) {
            if (userEntity.getCheckCertification() != 1) {
                return Result.error("请先实名认证");
            }
        }
        if (StringUtils.isNotBlank(postPush.getContent())) {
            String content = postPush.getContent().replaceAll("(\\d{3})\\d*([0-9a-zA-Z]{4})", "$1****$2");
            postPush.setContent(content);
        }
        if (postPush.getPostPushId() != null) {
            return Result.upStatus(baseMapper.updateById(postPush));
        } else {
            postPush.setCreateTime(LocalDateTime.now());
            postPush.setIsPush(0);
            return Result.upStatus(baseMapper.insert(postPush));
        }
    }

    @Override
    public Result deletePostPush(Long userId, Long postPushId) {
        PostPush postPush = baseMapper.selectById(postPushId);
        if (!postPush.getUserId().equals(userId)) {
            return Result.error("您无权操作当前信息");
        } else {
            return Result.upStatus(baseMapper.deleteById(postPushId));
        }

    }

    @Override
    public Result auditPostPush(Long postPushId, String content, Integer status) {
        PostPush postPush = baseMapper.selectById(postPushId);
        if (postPush == null) {
            return Result.error("当前信息不存在,请刷新后重试!");
        }
        UserEntity userEntity = userService.getById(postPush.getUserId());
        postPush.setStatus(status);
        postPush.setExamineContent(content);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        CommonInfo one = commonInfoService.findOne(67);
        List<String> msgList = new ArrayList<>();
        String postPushContent = postPush.getContent();
        if (postPushContent.length() >= 20) {
            postPushContent = postPushContent.substring(0, 15) + "...";
        }
        msgList.add(0, postPushContent);
        msgList.add(1, postPush.getCreateTime().format(fmt));
        if (status == 1) {
            msgList.add(2, "通过");
        } else if (status == 2) {
            msgList.add(2, "未通过");
        }
        if (content.length() >= 20) {
            content = content.substring(0, 15) + "...";
        }
        msgList.add(3, DateUtils.format(new Date()));
        msgList.add(4,content);
        if (StringUtils.isNotBlank(userEntity.getOpenId())) {
            SenInfoCheckUtil.sendMsg(userEntity.getOpenId(), one.getValue(), msgList, 1, null);
        }
        if (StringUtils.isNotBlank(userEntity.getClientid())) {
            userService.pushToSingle("审核结果", content, userEntity.getClientid());
        }

        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setTitle("审核通知");
        if (status == 1) {
            messageInfo.setContent("您的信息《"+postPush.getContent()+"》审核已通过");
        } else if (status == 2) {
            messageInfo.setContent("您的信息《"+postPush.getContent()+"》审核已未通过,可点击信息详情查看原因");
        }
        messageInfo.setState(String.valueOf(5));
        messageInfo.setUserId(String.valueOf(userEntity.getUserId()));
        messageInfo.setCreateAt(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        messageInfo.setIsSee("0");
        messageService.save(messageInfo);
        return Result.upStatus(baseMapper.updateById(postPush));
    }


    @Override
    public IPage<PostPushVo> getPostPushList(Integer page, Integer limit, PostPushVo postPushVo, String startTime, String endTime) {
        Page<PostPushVo> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.getPostPushList(pages, postPushVo, startTime, endTime);

    }

    @Override
    public List<LetterCity> getCityList(String city) {
        List<LetterCity> letterCityList = baseMapper.getLetter();
        for (LetterCity letterCity : letterCityList) {
            letterCity.setCity(baseMapper.getCityList(letterCity.getLetter(), city));
        }
        return letterCityList;
    }

    @Override
    public List<String> getCountyByCity(String city) {
        return baseMapper.getCountyByCity(city);
    }

    @Override
    public List<String> getHotCity(Integer limitCount) {
        return baseMapper.getHotCity(limitCount);

    }

    @Override
    public List<CityVo> getCityCounty() {
        List<CityVo> cityVos = baseMapper.getCityCounty();
        for (CityVo cityVo : cityVos) {
            cityVo.setCountyList(baseMapper.getCountyByCity(cityVo.getCity()));
        }
        return cityVos;
    }


    @Override
    public IPage<PostPushVo> getAppPostPushList(Integer page, Integer limit, Long loginUserId, PostPushVo postPushVo) {
        Page<PostPushVo> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<PostPushVo> pushVoIPage = baseMapper.getAppPostPushList(pages, loginUserId, postPushVo);
        for (PostPushVo record : pushVoIPage.getRecords()) {
            if (!record.getUserId().equals(loginUserId)) {
                record.setPhone(null);
            }
        }
        return pushVoIPage;
    }


    @Override
    public Result banPostPush(Long postPushId, String examineContent) {
        PostPush postPush = baseMapper.selectById(postPushId);
        if (postPush == null) {
            return Result.error("封禁的信息不存在");
        }
        postPush.setExamineContent(examineContent);
        if (postPush.getStatus() == 4) {
            postPush.setStatus(1);
        } else {
            postPush.setStatus(4);
        }
        return Result.upStatus(baseMapper.updateById(postPush));


    }

    @Override
    public String getPoshPushPhone(Long postPushId) {
        return baseMapper.selectById(postPushId).getPhone();


    }

    @Override
    public Result updateStatus(Long postPushId) {
        PostPush postPush = baseMapper.selectById(postPushId);
        if (postPush.getStatus() == 1) {
            postPush.setStatus(3);
        } else if (postPush.getStatus() == 3) {
            postPush.setStatus(1);
        } else {
            return Result.error("当前状态无法修改");
        }
        return Result.upStatus(baseMapper.updateById(postPush));

    }

    @Override
    public HashMap<String, Object> getPostPushData(String time, String flag) {
        HashMap<String, Object> hashMap = new HashMap<>();
        int allCount = baseMapper.getPostPushData(time, flag, null, null);
        hashMap.put("allCount", allCount);
        //招聘
        int zWaitExamine = baseMapper.getPostPushData(time, flag, 1, 0);
        int zXamineOk = baseMapper.getPostPushData(time, flag, 1, 1);
        int zExamineNo = baseMapper.getPostPushData(time, flag, 1, 2);
        int zFind = baseMapper.getPostPushData(time, flag, 1, 3);
        int zBan = baseMapper.getPostPushData(time, flag, 1, 4);
        hashMap.put("zWaitExamine", zWaitExamine);
        hashMap.put("zXamineOk", zXamineOk);
        hashMap.put("zExamineNo", zExamineNo);
        hashMap.put("zFind", zFind);
        hashMap.put("zBan", zBan);
        //求职
        int qWaitExamine = baseMapper.getPostPushData(time, flag, 2, 0);
        int qXamineOk = baseMapper.getPostPushData(time, flag, 2, 1);
        int qExamineNo = baseMapper.getPostPushData(time, flag, 2, 2);
        int qFind = baseMapper.getPostPushData(time, flag, 2, 3);
        int qBan = baseMapper.getPostPushData(time, flag, 2, 4);

        hashMap.put("qWaitExamine", qWaitExamine);
        hashMap.put("qXamineOk", qXamineOk);
        hashMap.put("qExamineNo", qExamineNo);
        hashMap.put("qFind", qFind);
        hashMap.put("qBan", qBan);

        return hashMap;


    }


    @Override
    public PostPushVo selectPostPushDetails(Long postPushId, Long loginUserId) {
        PostPushVo postPush = baseMapper.selectPostPushDetails(postPushId, loginUserId);
        if (!postPush.getUserId().equals(loginUserId)) {
            postPush.setPhone(null);
        }
        return postPush;

    }


}
